<?php


namespace app\admin\controller;



use app\lib\util\excel;
use think\facade\Db;
use think\facade\View;

class Trade extends Common
{
    public function statistic(){
        $com = $this->pidTokenId();
        $data=request()->get();
        $where=array();
        if(!empty($data)){

            if(!empty($data["community_id"])){
                $where[] = ['community_id','=',$data['community_id']];
            }
            if(!empty($data["start_time"])&&(empty($data["end_time"]))){
                $start_time=$data["start_time"];
                $where[] = ['date','>=',$start_time];
            }else if(empty($data["start_time"])&&(!empty($data["end_time"]))){
                $end_time=$data["end_time"];
                $where[] = ['date','<=',$end_time];
            }else if(!empty($data["start_time"])&&(!empty($data["end_time"]))){
                $start_time=$data["start_time"];
                $end_time=$data["end_time"];
                $where[] = ['date','>=',$start_time];
                $where[] = ['date','<=',$end_time];
            }
        }
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);

        $page = request()->has('page') ? request()->get('page') : 1;
        $list=Db::table("cloud_trade_statis")
            ->whereIn("community_id",$com["community_id"])
            ->where($where)
            ->order('id desc')->paginate( [
                'list_rows'=> 50,
                'page'  =>  $page,
                'query' =>  $where
            ]);;;
        $number=Db::table("cloud_trade_statis")
            ->whereIn("community_id",$com["community_id"])
            ->where($where)->count();
        $orderlist=array();
        foreach ($list as $key=> $info){
            $info["money"]=round($info["money_alipay"]+$info["money_wechat"],2);
            $orderlist[]=$info;
        }
        //var_dump($orderlist);
        View::assign("number",$number);
        foreach ($data as $key=> $value){
            View::assign($key,$value);
        }
        $pageRender=$list->render();
        View::assign('pageRender',$pageRender);
        View::assign('list',$orderlist);
        return View::fetch("statistic");

    }

    public function export(){
        $com = $this->pidTokenId();
        $export=request()->get("export");
        $export = str_replace("|","\"",$export);
        $where = json_decode($export,true);
        $list=Db::table("cloud_trade_statis")
            ->whereIn("community_id",$com["community_id"])
            ->where($where)
            ->limit(5000)
            ->order('id desc')
            ->select();
        $fileName="账单汇总";
        $title="账单汇总";
        $headArr = ['小区名', '商铺', '日期','支付宝','微信','合计'];
        $data=[];
        foreach ($list as $key=> $info){
            $value=[];
            $value["community_name"]=$info["community_name"];

            $value["store_name"]=$info["store_name"];
            $value["date"]=$info["date"];
            $value["money_alipay"]= $info["money_alipay"];
            $value["money_wechat"]=$info["money_wechat"];
            $value["money"]=round($info["money_alipay"]+$info["money_wechat"],2);
            $data[]=$value;
        }
        $excelObj = new excel();
        $excelObj->excelExport($title, $headArr, $data,$fileName);
    }
}
